package com.central.task.controller;

import com.central.task.entity.FlinkSqlTaskLog;
import com.central.task.entity.ShellTaskLog;
import com.central.task.entity.DataXTaskLog;
import com.central.task.entity.SqlTaskLog;
import com.central.task.model.TaskDefined;
import com.central.task.model.TaskDefinedLog;
import com.central.task.service.ITaskDefinedService;
import com.central.common.annotation.LoginUser;
import com.central.common.annotation.RequiredPermission;
import com.central.common.constant.PermissionConstants;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.common.model.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @author Tindy
 * @date 2021/11/25
 * @describe
 */
@Slf4j
@RestController
@Api(tags = "批量作业定义模块api")
@RequestMapping(value = "/taskDefined")
public class TaskDefinedController {

    @Autowired
    private ITaskDefinedService taskDefinedService;

    @ApiOperation(value = "创建或更新作业")
    @PostMapping("/saveOrUpdateTask")
    @RequiredPermission({PermissionConstants.DEV_USER,PermissionConstants.PRO_ADMIN})
    public Result saveOrUpdateTask(@ApiIgnore @LoginUser SysUser user, @RequestBody TaskDefined taskDefined){
        return taskDefinedService.saveOrUpdateTask(user,taskDefined);
    }

    @ApiOperation(value = "批量作业查询列表")

    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping("/findTasks")
    public PageResult<TaskDefined> findTasks(@ApiIgnore @LoginUser SysUser user, @RequestParam Map<String, Object> params) {
        return taskDefinedService.findTasks(user.getCurrentProjectId(), params);
    }

    @ApiOperation(value = "删除作业")
    @DeleteMapping("/delTask")
    @RequiredPermission({PermissionConstants.PRO_ADMIN,PermissionConstants.DEV_USER})
    public Result delTask(@ApiIgnore @LoginUser SysUser user,Long taskId){
        return taskDefinedService.delTask(user,taskId);
    }


    @ApiOperation(value = "保存Shell作业详情")
    @PostMapping("/saveShellTaskLog")
    public Result saveShellTaskLog(@ApiIgnore @LoginUser SysUser user,@RequestBody ShellTaskLog taskDefinedLog){
        //判断用户是否有编辑该作业的权限
        if(checkPermission(user,taskDefinedLog)){
            return taskDefinedService.saveTaskLog(taskDefinedLog);
        }else {
            return Result.failed("操作失败，权限不足");
        }
    }

    @ApiOperation(value = "临时执行shell作业")
    @PostMapping("/runShell")
    public Result runShell(@ApiIgnore @LoginUser SysUser user, @RequestBody ShellTaskLog taskDefinedLog){
        return taskDefinedService.tmpRunTask(taskDefinedLog);
    }

    @ApiOperation(value = "保存sql作业详情")
    @PostMapping("/saveSqlTaskLog")
    public Result saveSqlTaskLog(@ApiIgnore @LoginUser SysUser user,@RequestBody SqlTaskLog taskDefinedLog){
        //判断用户是否有编辑该作业的权限
        if(checkPermission(user,taskDefinedLog)){
            return taskDefinedService.saveTaskLog(taskDefinedLog);
        }else {
            return Result.failed("操作失败，权限不足");
        }
    }

    @ApiOperation(value = "临时执行sql作业")
    @PostMapping("/runSql")
    public Result runSql(@ApiIgnore @LoginUser SysUser user, @RequestBody SqlTaskLog taskDefinedLog){
        return taskDefinedService.tmpRunTask(taskDefinedLog);
    }

    @ApiOperation(value = "保存DataX作业详情")
    @PostMapping("/saveDataXTaskLog")
    public Result saveDataXTaskLog(@ApiIgnore @LoginUser SysUser user,@RequestBody DataXTaskLog taskDefinedLog){
        //判断用户是否有编辑该作业的权限
        if(checkPermission(user,taskDefinedLog)){
            return taskDefinedService.saveTaskLog(taskDefinedLog);
        }else {
            return Result.failed("操作失败，权限不足");
        }
    }

    @ApiOperation(value = "临时执行DataX作业")
    @PostMapping("/runDataX")
    public Result runDataX(@ApiIgnore @LoginUser SysUser user, @RequestBody DataXTaskLog taskDefinedLog){
        return taskDefinedService.tmpRunTask(taskDefinedLog);
    }

    @ApiOperation(value = "保存FlinkSql作业详情")
    @PostMapping("/saveFlinkSqlTaskLog")
    public Result saveFlinkSqlTaskLog(@ApiIgnore @LoginUser SysUser user,@RequestBody FlinkSqlTaskLog taskDefinedLog){
        //判断用户是否有编辑该作业的权限
        if(checkPermission(user,taskDefinedLog)){
            return taskDefinedService.saveTaskLog(taskDefinedLog);
        }else {
            return Result.failed("操作失败，权限不足");
        }
    }

    @ApiOperation(value = "临时执行FlinkSql作业")
    @PostMapping("/runFlinkSql")
    public Result runFlinkSql(@ApiIgnore @LoginUser SysUser user, @RequestBody FlinkSqlTaskLog taskDefinedLog){
        return taskDefinedService.tmpRunTask(taskDefinedLog);
    }

    @ApiOperation(value = "获取作业所有版本列表")
    @GetMapping("/findTaskLogs")
    public List<TaskDefinedLog> findTaskLogs(Long taskDefinedId){
        return taskDefinedService.findTaskLogs(taskDefinedId);
    }

    @ApiOperation(value = "作业版本发布")
    @GetMapping("/release")
    public Result release(@ApiIgnore @LoginUser SysUser user, Long taskDefinedLogId){
        TaskDefinedLog taskDefinedLog=taskDefinedService.getTaskLog(taskDefinedLogId);
        //判断用户是否有发布该作业的权限
        if(checkPermission(user,taskDefinedLog)){
            return taskDefinedService.release(taskDefinedLog);
        }else {
            return Result.failed("操作失败，权限不足");
        }
    }

    @ApiOperation(value = "删除作业版本")
    @DeleteMapping("/delTaskLog")
    @RequiredPermission({PermissionConstants.PRO_ADMIN,PermissionConstants.DEV_USER})
    public Result delTaskLog(@ApiIgnore @LoginUser SysUser user,Long taskLogId){
        return taskDefinedService.delTaskLog(user,taskLogId);
    }



    private boolean checkPermission(SysUser user, TaskDefinedLog taskDefinedLog) {
        List<String> owners = taskDefinedService.getOwners(taskDefinedLog.getTaskDefinedId());
        return owners.contains(user.getUsername());
    }
}
